package com.tencent.cloud.uikit.widget.keyboard;

import android.app.Activity;
import android.content.Context;
import android.content.res.ColorStateList;
import android.widget.Button;

import com.parkingwang.keyboard.KeyboardInputController;
import com.parkingwang.keyboard.OnInputChangedListener;
import com.parkingwang.keyboard.PopupKeyboard;
import com.parkingwang.keyboard.engine.KeyboardEngine;
import com.parkingwang.keyboard.view.InputView;
import com.parkingwang.keyboard.view.KeyboardView;

/**
 * @author vinsonhuang
 * @date 2018/9/21
 */
public class IovPopupKeyboard extends PopupKeyboard {

    private Activity mActivity;

    private OnInputCompletedListener mCompletedListener;

    public IovPopupKeyboard(Context context) {
        super(context);
    }

    public IovPopupKeyboard(Context context, int bubbleTextColor, ColorStateList
            okKeyBackgroundColor) {
        super(context, bubbleTextColor, okKeyBackgroundColor);
    }

    public IovPopupKeyboard(Activity activity, InputView inputView) {
        this(activity, inputView, null);
    }

    public IovPopupKeyboard(Activity activity, InputView inputView, Button lockTypeButton) {
        super(activity);
        mActivity = activity;
        attach(inputView, activity);
        init(lockTypeButton);
    }

    @Override
    public void attach(InputView inputView, Activity activity) {
        super.attach(inputView, activity);
    }

    @Override
    public KeyboardView getKeyboardView() {
        return super.getKeyboardView();
    }

    @Override
    public KeyboardInputController getController() {
        return super.getController();
    }

    @Override
    public KeyboardEngine getKeyboardEngine() {
        return super.getKeyboardEngine();
    }

    @Override
    public void show(Activity activity) {
        super.show(activity);
    }

    @Override
    public boolean isShown() {
        return super.isShown();
    }

    public void registerInputCompletedListener(OnInputCompletedListener listener) {
        mCompletedListener = listener;
        getController().addOnInputChangedListener(new OnInputChangedListener() {

            @Override
            public void onChanged(String s, boolean isCompleted) {
                if (null != mCompletedListener) {
                    mCompletedListener.onChanged(s, isCompleted);
                }
            }

            @Override
            public void onCompleted(String s, boolean b) {
                IovPopupKeyboard.this.dismiss(mActivity);
            }
        });
    }

    public void unregisterInputCompletedListener() {
        mCompletedListener = null;
    }

    private void init(Button lockTypeButton) {
        getController().setDebugEnabled(true);
        if (null == lockTypeButton) {
            return;
        }
        getController().bindLockTypeProxy(
                new KeyboardInputController.ButtonProxyImpl(lockTypeButton) {
                    @Override
                    public void onNumberTypeChanged(boolean isNewEnergyType) {
                        super.onNumberTypeChanged(isNewEnergyType);
                    }
                });
    }

    public interface OnInputCompletedListener {
        void onChanged(String s, boolean isCompleted);
    }
}
